//
//  MainViewController.m
//  MyCentenDemo
//
//  Created by 徐凯 on 2017/6/22.
//  Copyright © 2017年 XK. All rights reserved.
//

#import "MainViewController.h"
#import "ChildViewController.h"
#import "UINavigationBar+Awesome.h"
#import <objc/runtime.h>
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define NAVBAR_CHANGE_POINT 64

@interface MainViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIScrollView *contentScrollView;

@property (nonatomic, strong) ChildViewController *childVC;

@property (nonatomic, strong) UIView *selectView;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    [self setupNav];
    [self setupUI];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self scrollViewDidScroll:_scrollView];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}


-(void) setupNav
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftBtnClick)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void) leftBtnClick
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


-(void) setupUI
{
    __weak typeof(self) weakSelf = self;
    _childVC = [[ChildViewController alloc] init];
    _childVC.IsScroll = ^{
        weakSelf.scrollView.scrollEnabled = YES;
    };
    [self addChildViewController:_childVC];
    
    
    
    self.view.backgroundColor = [UIColor orangeColor];
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.backgroundColor = [UIColor grayColor];
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(0, ScreenHeight + 200);
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 200)];
    _headView.backgroundColor = [UIColor yellowColor];
    [_scrollView addSubview:_headView];
    
    _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _headView.bottom, ScreenWidth, ScreenHeight)];
    _contentScrollView.backgroundColor = [UIColor greenColor];
    [_scrollView addSubview:_contentScrollView];
    
    
    [_contentScrollView addSubview:_childVC.view];
    _childVC.view.frame = CGRectMake(0, 0, _contentScrollView.width, _contentScrollView.height);
    _childVC.tableView.scrollEnabled = NO;
    
    
    
    _selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    _selectView.bottom = _headView.height;
    _selectView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_selectView];
    
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == _scrollView)
    {
        
        UIColor * color = [UIColor whiteColor];//[UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY > NAVBAR_CHANGE_POINT) {
            CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
            
        } else {
            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
            
            
        }
       
        
        if(offsetY > (_headView.height - 64 - _selectView.height))
        {
            _selectView.top = 64;

        }
        else
        {
            CGRect rect = [_headView convertRect:_headView.frame fromView:self.view];
            
            _selectView.top = -rect.origin.y + (_headView.height - _selectView.height);
            
            NSLog(@"%@", NSStringFromCGRect(rect));
        }
        
        if(offsetY > _headView.height)
        {
            _scrollView.scrollEnabled = NO;
            _childVC.tableView.scrollEnabled = YES;
        }
        else
        {
            

        }
        
        
        
    }
}


@end
